package com.stars.empoly.vmodel

import android.app.Application
import android.text.Editable
import android.text.TextUtils
import androidx.databinding.Bindable
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.stars.empoly.BR
import com.stars.empoly.model.BannerBean
import com.stars.empoly.model.ServiceList
import com.stars.empoly.utils.net.NetEngine
import com.stars.empoly.view.SimpleTextWatcher
import com.trailer.base.base.BRRepository
import com.trailer.base.base.BRViewModel
import com.trailer.base.base.BRepository
import com.trailer.base.base.BViewModel
import com.trailer.base.net.IResp
import kotlinx.coroutines.launch


/**
 *
 *on 2020/9/23
 */
open class HomeClientVModel(application: Application) : BViewModel<BRepository>(application) {

    var type=MutableLiveData<String>()
    var bannerList=MutableLiveData<List<BannerBean>>()

    var bean=MutableLiveData<ServiceList>()

    var keyword:String?=null
    var sortCondition:String?=null

//    @Bindable
//    open var k: String? =""
//        set(value) {
//            field = value
//            notifyPropertyChanged(BR.k)
//        }


    var oldText = ""
    var textWatcher = object : SimpleTextWatcher() {
        override fun afterTextChanged(s: Editable?) {
            super.afterTextChanged(s)
            var content = s.toString()
            if (!TextUtils.isEmpty(oldText) && content == oldText) {
                return
            }
            keyword = content
            oldText = content
            //刷新
            getInfo()

        }

    }



    fun getBanner(){
        viewModelScope.launch {
            var it=repository().lreq { NetEngine.service.bannerList() }
            if(it.ok()){
                bannerList.value=it.data
            }
        }

    }

    fun getInfo(){
        viewModelScope.launch{
            var it= NetEngine.service.platformServiceList(null,keyword,sortCondition)
            if(it.ok()){
                bean.value=it.data
                view().refreshing(false)
            }else{
                view().refreshing(false)
            }
        }
    }




}
